package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import com.augurit.gzcgwsp.ywcmn.convert.BaseDistrictConvert;
import com.augurit.gzcgwsp.ywcmn.dao.BaseDistrictDao;
import com.augurit.gzcgwsp.ywcmn.entity.BaseDistrict;
import com.augurit.gzcgwsp.ywcmn.service.IBaseDistrictService;
import com.augurit.gzcgwsp.ywcmn.web.form.BaseDistrictForm;
/**
 * 地区配置表
 * @author Administrator
 *
 */
@Service
@Transactional
public class BaseDistrictServiceImpl implements IBaseDistrictService{
	@Autowired
	private BaseDistrictDao baseDistrictDao;
	
	public List getValue(String parentDistrictId) {
		String hql = "select t.id,t.districtName,t.districtCode from BaseDistrict t where t.parentDistrictId=" + parentDistrictId;
		List list = baseDistrictDao.find(hql);
		return list;
	}

	public List getValue(){
		String hql = "select t.id,t.districtName,t.districtCode,t.parentDistrictId from BaseDistrict t ";
		List list = baseDistrictDao.find(hql);
		return list;		
	}
	
	public Page<BaseDistrictForm> search(Page<BaseDistrictForm> page,
			BaseDistrictForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<BaseDistrictForm> search(Page<BaseDistrictForm> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Long... ids) {
		baseDistrictDao.delete(ids);
		
	}

	public BaseDistrictForm get(Long id) {
		BaseDistrict entity = baseDistrictDao.get(id);

		return BaseDistrictConvert.convertVoToForm(entity);
	}

	public void save(BaseDistrictForm... forms) {
		if (forms != null) {
			for(BaseDistrictForm form:forms)
				this.save(form);
		}
	}
	
	public void save(BaseDistrictForm form){
		if (form != null) {
			BaseDistrict entity = null;
			if (form != null && form.getId() != null) {
				entity = baseDistrictDao.get(form.getId());
			}else {
				entity = new BaseDistrict();
			}
			BaseDistrictConvert.convertFormToVo(form, entity);
			baseDistrictDao.save(entity);
			form.setId(entity.getId());
		}
	}
	
	/**
	 * 通过区名称获取区ID
	 * @param districtName
	 * @return districtId
	 */
	public Long getBaseDistrictIdByDistrictName(String districtName){
		String hql = "select t.id from BaseDistrict t where t.districtName='" + districtName + "'";
		List list = baseDistrictDao.find(hql);
		Long districtId = null;
		if(list.size()>0){
			districtId = (Long)list.get(0);
		}
		return districtId;
	}
	
	/**
	 * 通过ID获取层次  返回1，为【区】；返回2，为【街道】
	 * @param id
	 * @return districtLevel
	 */
	public Integer getDistrictLevelById(Long id){
		String hql = "select t.districtLevel from BaseDistrict t where t.id=" + id + "";
		List list = baseDistrictDao.find(hql);
		Integer districtLevel = null;
		if(list.size()>0){
			districtLevel = (Integer)list.get(0);
		}
		return districtLevel;
	}
	
	/**
	 * 通过区的名称和区级别，模糊查询区的id
	 * @param districtName 区名称
	 * @return id
	 */
	public Long getBaseDistrictIdByDistrictNameAndDistrictLevel(String districtName){
		String hql = "select t.id from BaseDistrict t where t.districtName like '%" + districtName + "%' and t.districtLevel='1'";
		List list = baseDistrictDao.find(hql);
		Long districtId = null;
		if(list.size()>0){
			districtId = (Long)list.get(0);
		}
		return districtId;
	}
	
	
	
}
